using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Hadron
{
    class GameStateManager
    {
        private GameState m_gameState;

        public GameStateManager(GameState p_gameStateInitial, GameContext p_gameContext)
        {
            m_gameState = p_gameStateInitial;
            m_gameState.PreviousState = m_gameState.NextState = null;
            m_gameState.Context = p_gameContext;
            m_gameState.OnEnter();
        }

        public void Update(GameTime p_gameTime)
        {
            if (m_gameState == null)
                return;

            // handle state switch
            if (m_gameState.NextState != null
                && m_gameState.NextState != m_gameState)
            {
                m_gameState.OnExit();

                GameState gameStateNew = m_gameState.NextState;

                m_gameState.NextState = m_gameState.PreviousState = null;
                gameStateNew.PreviousState = m_gameState;
                gameStateNew.NextState = null;
                gameStateNew.Context = m_gameState.Context;

                m_gameState = gameStateNew;
                m_gameState.OnEnter();
            }

            // update
            m_gameState.Update(p_gameTime);
        }

        public void Draw(SpriteBatch p_spriteBatch)
        {
            if (m_gameState == null)
                return;

            m_gameState.Draw(p_spriteBatch);
        }
    }
}
